package net.melon.article.inst.struts2;

import java.util.List;

import javax.annotation.Resource;

import net.esj.basic.utils.CollectionUtils;
import net.esj.basic.utils.StringUtils;
import net.esj.basic.view.AbstractPluginPageAction;
import net.melon.article.ArticleConstants;
import net.melon.article.manager.ArticleManage;
import net.melon.article.pojo.Article;
import net.melon.article.pojo.Channel;
import net.melon.uploader.ErrorInfo;
import net.melon.uploader.Uploader;
import net.melon.uploader.exception.UploadRuntimeException;
import net.melon.uploader.pojo.FileUpload;
import net.melon.uploader.service.FileNameRule;
import net.melon.uploader.service.PathRule;
import net.melon.uploader.service.impl.DateSplitPathRule;
import net.melon.uploader.service.impl.RandomFileNameRule;

public class ArticleAction extends AbstractPluginPageAction<Article, Article>{

	/**
	 * 
	 */
	private static final long serialVersionUID = 2493439333175345888L;
	
	private int channel;
	
	private PathRule pathRule = new DateSplitPathRule();
	
	private FileNameRule fileNameRule = new RandomFileNameRule();
	
	Uploader uploader = new Uploader(pathRule, fileNameRule, ArticleConstants.MELON_ARTICLE_UPLOADER_KEY);
	
	@Resource
	private ArticleManage articleManage;
	
	@Override
	protected void doQueryPage() {
		if(channel!=0){
			pagination.getQueryBuilder().eq("channel.id", channel);
			super.doQueryPage();
		}
	}
	
	
	public String articleSelect(){
		return SUCCESS;
	}
	
	public List<Channel> getChannelList(){
		List<Channel> list = articleManage.getChannelList();
		return list; 
	}
	
	@Override
	protected void doAdd() throws Exception {
		articleManage.saveArticle(entity, getOperator());
	}
	
	@Override
	protected void doGetEntity() {
		entity = defaultService.get(Integer.valueOf(id), Article.class);
	}
	
	@Override
	protected void doUpdate() throws Exception {
		articleManage.updateArticle(entity);
	}
	
	public void upload() throws Exception{
		try{
			uploader.setAllowFiles(new String[]{".jpg",".png",".gif",".jpeg",".bmp"});
			List<FileUpload> list = uploader.upload(getRequest());
			if(!CollectionUtils.isEmpty(list)){
				FileUpload fu = list.get(0);
				writeString("{'state':'"+ErrorInfo.SUCCESS+"','url':'"+fu.getUrl()+"','original':'"+fu.getOriginal()+"'}");
			}else{
				writeString("{state:'"+ErrorInfo.UNKNOW+"'}");
			}
		}catch(UploadRuntimeException e){
			writeString("{state:'"+e.getMessage()+"'}");
		}
	}
	
	public void imageManager(){
		String[] files = uploader.getDownFiles();
		writeString(StringUtils.join(files, "ue_separate_ue"));
	}
	
	public String getDownloadUrl(){
		return uploader.getDownUrl(ArticleConstants.MELON_ARTICLE_UPLOADER_KEY);
	}
	
	public String getIconUrl(){
		return uploader.getDownUrl(ArticleConstants.ARTICLE_CONTENT_ICON_UPLOAD_KEY);
	}
	
	@Override
	protected void doDelete() throws Exception {
		articleManage.deleteArticle(Integer.valueOf(deletes));
	}

	public Article getEntity(){
		return entity;
	}
	public void setEntity(Article entity){
		this.entity = entity;
	}
	public int getChannel() {
		return channel;
	}
	public void setChannel(int channel) {
		this.channel = channel;
	}
	
	
}
